var JSObject = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add:true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Sender', name:'user_id', width:150, sortable:true, align:'left', hide:false},
                {display:'Send To Telephone', name:'fon_number', width:150, sortable:true, align:'left', hide:false},
                {display:'Message', name:'message', width:300, sortable:true, align:'left', hide:false},
                {display:'Status', name:'status', width:150, sortable:true, align:'left', hide:false},
                {display:'Date Sent', name:'created', width:150, sortable:true, align:'left', hide:false}
            ],
           /* formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent}
            ],
            searchitems:[
                {display:'Customer Name', name:'name', isdefault:true},
                {display:'Telephone', name:'telephone'},
                {display:'Email', name:'email'},
                {display:'Address', name:'address'}
             ],*/
            checkboxSelection:false,
            editablegrid:{
                use:false
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

        $('#fon_numbers').tagsInput({
            'defaultText':'add number',
            'placeholderColor' : '#cac260',
            'width':'250px',
            onChange: function(elem, elem_tags)
            {
                $('.fon_numbers', elem_tags).each(function()
                {
                    $(this).css('background-color', '#cac260');

                });
            }
        });

        $("#message").charCount({
            allowed: 160,
            warning: 0,
            counterText: 'Note that network operators will charge for every 160 characters as one message. Characters count: '
        });

        $("#sms-modal-btn").click(function(){
            $('#sms_modal').modal({
                backdrop: 'static',
                show: true,
                keyboard: true
            });
        });
        $("#close_sms_btn").click(function(){
            document.getElementById('form-sms').reset();
        });


        $("#send_sms_btn").click(function(){
            self.sendSMS($(this).attr('href'));
        });

    },


    sendSMS:function(url){
        var self = this;
        var numbers = $("#form-sms #fon_numbers").val();
        var message = $("#form-sms #message").val();
        var numbers = numbers.trim();
        var message = message.trim();
        if(message.length == 0 || numbers.length == 0){
            jLib.serverError("Please enter at least one phone number and a message to send to.");
            return;
        }

        var query = $("#sms_modal #form-sms").serialize();
        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(response) {
                var txt = '';
                if (typeof response.msg == 'object') {
                    for (var megTxt in response.msg) {
                        txt += response.msg[megTxt] + '<br />';
                    }
                }
                else {
                    txt = response.msg
                }
                if(response.code == 0){
                    jLib.serverSuccess(txt);
                    $("#close_sms_btn").click();
                    $(self.objGrid).flexReload();
                }
                else{
                    jLib.serverError(txt);
                }
            },
            error: function() {
                jLib.serverError();
            }
        });
    },

   /* handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            JSObject.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid,1);
            if(r){
                var row = FlexObject.getSelectedRows(grid);
                JSObject.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if (com == 'Save') {
            JSObject.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            JSObject.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid);
            if(r){
                JSObject.delete_(grid);
            }
        }
        else if (com == 'Export All') {
            var url = $("#export_url").val();
            window.open(url, "PrintExportWindow", "menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        }
    },*/

    delete_:function (grid) {
        var self = this;
        var url = $('#delete_url').val();
        jLib.do_delete(url, grid,function(response){
            if(response.code == 0){
                FlexObject.removeGridRows(grid);
                self.objGrid.flexNotify('Status', response.msg, 'success');
            }
            else if(response.code == 1){
                self.objGrid.flexNotify('Status', response.msg, 'error');
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    JSObject.init();
});